package com.woniu.cms.goods.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.cms.goods.core.entity.*;
import com.woniu.cms.goods.core.mapper.GoodsBalanceMapper;
import com.woniu.cms.goods.core.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.BusinessCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 阅读材料平衡信息报表 服务实现类
 * </p>
 *
 * @author romantic sheep
 * @since 2022-09-19
 */
@Service
public class GoodsBalanceServiceImpl extends ServiceImpl<GoodsBalanceMapper, GoodsBalance> implements IGoodsBalanceService {
    @Autowired
    private GoodsBalanceMapper balanceMapper;
    
    @Override
    public List<GoodsBalanceMaterial> countGoods(String projectCode, Integer materialType) {
        List<GoodsBalanceMaterial> list = balanceMapper.countGoods(projectCode,materialType);
        if (list.isEmpty()){
            return null;
        }
        BigDecimal d = new BigDecimal(0);
        List<GoodsBalanceMaterial> collect = list.stream().map(e -> {
            if (e.getGetbillNum()==null){
                e.setGetbillNum(d);
            }
            if (e.getOutbillNum()==null){
                e.setOutbillNum(d);
            }
            if (e.getPaybackNum()==null){
                e.setPaybackNum(d);
            }
            if (e.getReturnNum()==null){
                e.setReturnNum(d);
            }
            if (e.getSettleNum()==null){
                e.setSettleNum(d);
            }
            if (e.getGetbillPrice()==null){
                e.setGetbillPrice(d);
            }
            BigDecimal profitAndLossNum = e.getGetbillNum().subtract(e.getOutbillNum())
                    .subtract(e.getPaybackNum())
                    .add(e.getReturnNum())
                    .subtract(e.getSettleNum());
            e.setProfitAndLossNum(profitAndLossNum);
            e.setProfitAndLoss(profitAndLossNum.multiply(e.getGetbillPrice()));
            return e;
        }).collect(Collectors.toList());
        return collect;
    }
}
